package com.itheima;

import com.itheima.service.AccountService;
import com.itheima.service.impl.AccountServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Mind {
    //演示思路
    public static void main(String[] args) {

        //
        AccountService accountService=new AccountServiceImpl();



        //学过啥?? 动态代理

        AccountService proxyInstance = (AccountService) Proxy.newProxyInstance(
                Mind.class.getClassLoader(),//类加载器
                new Class[]{AccountService.class},
                new myInvocationHandler(accountService)
        );


        proxyInstance.delete(1);

        proxyInstance.findById(1);


    }

    private static class myInvocationHandler implements InvocationHandler{
        public myInvocationHandler(AccountService ori) {
            this.ori = ori;
        }

        private AccountService ori;

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            //System.out.println("我才是真正的干活的人");

            //不管都要记得 返回值
            Object result=null;

            long start = System.currentTimeMillis();

            //使用原本的对象调用方法
            result=method.invoke(ori, args);


            long end = System.currentTimeMillis();
            System.out.println(method.getName()+"在运行的时候共花费了:"+(end-start)+"毫秒");


            return result;
        }
    }





}
